package com.kuaifawu.lwnlawyerclient.Adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.kuaifawu.lwnlawyerclient.Activity.LWNQuestionDetailActivity;
import com.kuaifawu.lwnlawyerclient.Lib.CircularImage;
import com.kuaifawu.lwnlawyerclient.Lib.LWNDataLoader;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_comment;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_questionAdd;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_questionDetail;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangminglei on 15/1/22.
 */
public class LWNAdapter_questionDetail extends ArrayAdapter {
    private LayoutInflater mInflater;
    private Context context_this;
    private LWNModel_questionDetail model_questionDetail;

    public ArrayList<LWNModel_questionAdd> array_add;
    private LWNQuestionDetailActivity activity;

    final int TYPE_0 = 0;
    final int TYPE_1 = 1;
    final int TYPE_2 = 2;
    final int TYPE_3 = 3;

    public LWNAdapter_questionDetail(Context context, int resource, List<LWNModel_questionDetail> objects,LWNQuestionDetailActivity activity) {
        super(context, resource, objects);
        context_this = context;
        this.mInflater = LayoutInflater.from(context);
        this.model_questionDetail = objects.get(0);
        this.activity=activity;
    }

    @Override
    public int getCount() {
        int position = 1;
        ArrayList<String> array_img_thumb = model_questionDetail.getImg_thumbnai();
        ArrayList<LWNModel_questionAdd> array_questionadd = model_questionDetail.getArray_questionadd();
      //  ArrayList<String> arrayimg_thumb = model_questionDetail.getArray_questionadd().get()
        if(array_img_thumb != null){
            position = position + 1;
        }

        if (array_questionadd != null){
            position = position + array_questionadd.size();
        }

        LWNModel_comment model_comment = model_questionDetail.getComment();
        if(model_comment != null){
            position = position + 1;
        }
        return position;
    }

    @Override
    public int getItemViewType(int position) {
         ArrayList<String> array_img_thumb = model_questionDetail.getImg_thumbnai();
         LWNModel_comment model_comment = model_questionDetail.getComment();
         if(array_add == null) {
             array_add = model_questionDetail.getArray_questionadd();
         }
         if(position == 0){
             return TYPE_0;
         }
         else if (array_img_thumb != null && position == 1){
             return TYPE_1;
         } else if (position == getCount() - 1 && model_comment != null) {
             return TYPE_3;
         }else{
             LWNModel_questionAdd model_questionAdd = null;
             if (array_img_thumb != null) {
                 model_questionAdd = array_add.get(position - 2);
             } else {
                 model_questionAdd = array_add.get(position - 1);
             }

             int type = Integer.parseInt(model_questionAdd.getType());
             if(type == 0){
                 return TYPE_2;
             }
             else{//客户追问发送的图片的情况
                 if(model_questionAdd.getImg_thum()!=null){
                     return TYPE_1;
                 }
                 return TYPE_0;
             }
         }
    }

    @Override
    public LWNModel_questionDetail getItem(int position) {
        return model_questionDetail;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String[] string_location = {"北京", "上海", "天津", "重庆", "黑龙江省", "吉林省", "辽宁省", "河北省", "山西省", "内蒙古", "山东省",
                "安徽省", "江苏省", "浙江省", "江西省", "福建省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆",
                "香港", "澳门", "台湾省"};

        LWNView_wenContent view_wenContent = null;
        LWNView_wenImage view_wenImage = null;
        LWNView_daContent view_daContent = null;

        LWNModel_comment model_comment = model_questionDetail.getComment();

        int location = Integer.parseInt(model_questionDetail.getQuestion_area());
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_0: {
                    convertView = mInflater.inflate(R.layout.item_qd_mymessage, parent, false);
                    view_wenContent = new LWNView_wenContent();
                    view_wenContent.textView_time = (TextView) convertView.findViewById(R.id.wen_time);
                    view_wenContent.textView_name = (TextView) convertView.findViewById(R.id.wen_name);
                    view_wenContent.textView_content = (TextView) convertView.findViewById(R.id.wen_content);
                    convertView.setTag(R.layout.item_qd_mymessage,view_wenContent);
                }
                break;
                case TYPE_1: {
                    convertView = mInflater.inflate(R.layout.item_dp_myimage, parent, false);
                    view_wenImage = new LWNView_wenImage();
                    view_wenImage.textView_name = (TextView) convertView.findViewById(R.id.wen_name);
                    view_wenImage.imageView_one = (ImageView) convertView.findViewById(R.id.qd_imageOne);
                    view_wenImage.imageView_two = (ImageView) convertView.findViewById(R.id.qd_imageTwo);
                    convertView.setTag(R.layout.item_dp_myimage,view_wenImage);
                }
                break;
                case TYPE_2: {
                    convertView = mInflater.inflate(R.layout.item_dq_youmessage, parent, false);
                    view_daContent = new LWNView_daContent();
                    view_daContent.textView_name = (TextView) convertView.findViewById(R.id.lawyer_name);
                    view_daContent.textView_content = (TextView) convertView.findViewById(R.id.lawyer_content);
                    view_daContent.textView_time = (TextView) convertView.findViewById(R.id.lawyer_time);
                    view_daContent.imageView_portrait = (CircularImage) convertView.findViewById(R.id.lawyer_touxiang);
                    convertView.setTag(R.layout.item_dq_youmessage,view_daContent);
                }
                break;
                case TYPE_3:{
                    convertView = mInflater.inflate(R.layout.item_qd_myrating, parent, false);
                }break;
                default: {

                }
                break;
            }
        }
        else {
            switch (type) {
                case TYPE_0: {
                    view_wenContent = (LWNView_wenContent)convertView.getTag(R.layout.item_qd_mymessage);
                    if(view_wenContent == null){
                        convertView = mInflater.inflate(R.layout.item_qd_mymessage, parent, false);
                        view_wenContent = new LWNView_wenContent();
                        view_wenContent.textView_time = (TextView) convertView.findViewById(R.id.wen_time);
                        view_wenContent.textView_name = (TextView) convertView.findViewById(R.id.wen_name);
                        view_wenContent.textView_content = (TextView) convertView.findViewById(R.id.wen_content);
                        convertView.setTag(R.layout.item_qd_mymessage, view_wenContent);
                    }
                }break;
                case TYPE_1: {
                    view_wenImage = (LWNView_wenImage)convertView.getTag(R.layout.item_dp_myimage);
                    if(view_wenImage == null){
                        convertView = mInflater.inflate(R.layout.item_dp_myimage, parent, false);
                        view_wenImage = new LWNView_wenImage();
                        view_wenImage.textView_name = (TextView) convertView.findViewById(R.id.wen_name);
                        view_wenImage.imageView_one = (ImageView) convertView.findViewById(R.id.qd_imageOne);
                        view_wenImage.imageView_two = (ImageView) convertView.findViewById(R.id.qd_imageTwo);


                        convertView.setTag(R.layout.item_dp_myimage,view_wenImage);
                    }
                }break;
                case TYPE_2: {
                    view_daContent = (LWNView_daContent)convertView.getTag(R.layout.item_dq_youmessage);
                    if(view_daContent == null){
                        convertView = mInflater.inflate(R.layout.item_dq_youmessage, parent, false);
                        view_daContent = new LWNView_daContent();
                        view_daContent.textView_name = (TextView) convertView.findViewById(R.id.lawyer_name);
                        view_daContent.textView_content = (TextView) convertView.findViewById(R.id.lawyer_content);
                        view_daContent.textView_time = (TextView) convertView.findViewById(R.id.lawyer_time);
                        view_daContent.imageView_portrait = (CircularImage) convertView.findViewById(R.id.lawyer_touxiang);
                        convertView.setTag(R.layout.item_dq_youmessage, view_daContent);
                    }
                }break;
                case TYPE_3: {
                    convertView = mInflater.inflate(R.layout.item_qd_myrating, parent, false);
                }break;
                default: {

                }break;
            }
        }

        //设置数据
        ArrayList<String> array_img_thumb = model_questionDetail.getImg_thumbnai();
        switch (type) {
            case TYPE_0: {
                if(position == 0) {
                    view_wenContent.textView_name.setText(string_location[location] + "\n" + "网友");
                    view_wenContent.textView_time.setText(model_questionDetail.getTime());
                    view_wenContent.textView_content.setText(model_questionDetail.getQuestion());
                }
                else{
                    LWNModel_questionAdd model_questionAdd = null;
                    if (array_img_thumb != null) {
                        model_questionAdd = array_add.get(position - 2);
                    } else {
                        model_questionAdd = array_add.get(position - 1);
                    }
                    view_wenContent.textView_name.setText(string_location[location] + "\n" + "网友");
                    view_wenContent.textView_content.setText(model_questionAdd.getContent());
                    view_wenContent.textView_time.setText(model_questionAdd.getTime());
                }
            }break;
            case TYPE_1: {
                view_wenImage.textView_name.setText(string_location[location] + "\n" + "网友");
                BitmapUtils bitmapUtils = new BitmapUtils(context_this);
                if(array_img_thumb!=null){
                    bitmapUtils.display(view_wenImage.imageView_one, array_img_thumb.get(0));
                    view_wenImage.imageView_two.setVisibility(View.GONE);
                    if(array_img_thumb.size()>1){
                        view_wenImage.imageView_two.setVisibility(View.VISIBLE);
                        bitmapUtils.display(view_wenImage.imageView_two, array_img_thumb.get(1));
                    }
                }
              LWNModel_questionAdd model_questionAdd=null;
                if(array_img_thumb != null&&position>1){
                    model_questionAdd = array_add.get(position - 2);
                }else if(array_img_thumb == null&&position>0){
                    model_questionAdd = array_add.get(position - 1);
                }
              final   ArrayList<String> arrayimagenew;
                if(model_questionAdd!=null){
                    final ArrayList<String> arrayimage=model_questionAdd.getContentadd_img();
                    arrayimagenew=arrayimage;
                }else{
                    arrayimagenew=null;
                }

                if(model_questionAdd!=null&&model_questionAdd.getImg_thum()!=null){
                    bitmapUtils.display(view_wenImage.imageView_one, model_questionAdd.getImg_thum().get(0));
                    view_wenImage.imageView_two.setVisibility(View.GONE);
                    if(model_questionAdd.getImg_thum().size()>1){
                        view_wenImage.imageView_two.setVisibility(View.VISIBLE);
                        bitmapUtils.display(view_wenImage.imageView_two, model_questionAdd.getImg_thum().get(1));
                    }
                }

                newbitmapUtils= new BitmapUtils(context_this);
                view_wenImage.imageView_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(position<=1){
                            getPopupShowImg(model_questionDetail.getQuestion_img(),0);
                        }else{
                            getPopupShowImg(arrayimagenew,0);
                        }

                    }
                });

                view_wenImage.imageView_two.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //getPopupShowImg(model_questionDetail.getQuestion_img(),1);
                        if(position<=1){
                            getPopupShowImg(model_questionDetail.getQuestion_img(),1);
                        }else{
                            getPopupShowImg(arrayimagenew,1);
                        }

                    }
                });
            }break;
            case TYPE_2: {
                LWNModel_questionAdd model_questionAdd;
                if(array_img_thumb != null){
                    model_questionAdd = array_add.get(position - 2);
                }
                else{
                    model_questionAdd = array_add.get(position - 1);
                }
                view_daContent.textView_name.setText(model_questionAdd.getRealname() + "\n" + "律师");
                view_daContent.textView_content.setText(model_questionAdd.getContent());
                view_daContent.textView_time.setText(model_questionAdd.getTime());
                BitmapUtils bitmapUtils = new BitmapUtils(context_this);
                if (model_questionAdd.getHead_src() != null) {
                    bitmapUtils.display(view_daContent.imageView_portrait, model_questionAdd.getHead_src());
                }
            }break;
            case TYPE_3: {
                ImageView imageView_portrait_one = (ImageView)convertView.findViewById(R.id.lvshi_touxiang_one);
                ImageView imageView_portrait_two = (ImageView)convertView.findViewById(R.id.lvshi_touxiang_two);
                RatingBar ratingBar_xy = (RatingBar)convertView.findViewById(R.id.item_qd_xy);
                RatingBar ratingBar_fw = (RatingBar) convertView.findViewById(R.id.item_qd_fw);
                RatingBar ratingBar_zy = (RatingBar) convertView.findViewById(R.id.item_qd_zy);
                ImageView imageView_medal = (ImageView)convertView.findViewById(R.id.xunzhang);
                TextView textView_comment = (TextView)convertView.findViewById(R.id.pingjia);
                TextView textView_nameOne = (TextView)convertView.findViewById(R.id.yonghu_name_one);
                TextView textView_nameTwo = (TextView) convertView.findViewById(R.id.yonghu_name_two);
                TextView textView_time = (TextView)convertView.findViewById(R.id.wen_time);

                int adpot = Integer.parseInt(model_comment.getAutomatic_adopt());

                if(adpot == 0){
                    //用户采纳
                    imageView_portrait_one.setImageDrawable(context_this.getResources().getDrawable(R.drawable.qd_mytpuxiang));
                    imageView_portrait_two.setImageDrawable(context_this.getResources().getDrawable(R.drawable.qd_mytpuxiang));
                    imageView_medal.setImageDrawable(context_this.getResources().getDrawable(R.drawable.medal_people));
                    textView_comment.setText(model_comment.getContent());
                    textView_nameOne.setText(string_location[location] + "\n" + "网友");
                    textView_nameTwo.setText(string_location[location] + "\n" + "网友");

                }else{
                    //系统采纳
                    imageView_portrait_one.setImageDrawable(context_this.getResources().getDrawable(R.drawable.qd_systemtouxiang));
                    imageView_portrait_two.setImageDrawable(context_this.getResources().getDrawable(R.drawable.qd_systemtouxiang));
                    imageView_medal.setImageDrawable(context_this.getResources().getDrawable(R.drawable.medal_system));
                    textView_comment.setText(model_comment.getContent());
                    textView_nameOne.setText("来问\n小助手");
                    textView_nameTwo.setText("来问\n小助手");
                }

                ratingBar_xy.setRating(Float.parseFloat(model_comment.getXy_fraction()));
                ratingBar_fw.setRating(Float.parseFloat(model_comment.getFuwu_fraction()));
                ratingBar_zy.setRating(Float.parseFloat(model_comment.getZy_fraction()));
                textView_time.setText(model_comment.getComment_time());
            }break;
            default: {

            }break;
        }



        return convertView;
    }

    class LWNView_wenContent {
        TextView textView_name;
        TextView textView_time;
        TextView textView_content;
    }

    class LWNView_wenImage {
        ImageView imageView_one;
        ImageView imageView_two;
        TextView textView_name;
    }

    class LWNView_daContent {
        TextView textView_name;
        TextView textView_time;
        CircularImage imageView_portrait;
        TextView textView_content;
    }


    private ViewPager detail_show_image=null;
    private ImageButton detail_showback_image=null;
    private PopupWindow showpopupWindow=null;
   private  BitmapUtils newbitmapUtils =null;
    private LinearLayout line_intor=null;
    private ImageButton line_intor_two=null;
    private ImageButton line_intor_one=null;
    private ProgressBar progress_bar;
    List<View> viewList=new ArrayList<View>();
    private void getPopupShowImg( final ArrayList<String> array_img,int current){
      ((InputMethodManager)  activity.getSystemService(activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
        viewList.clear();
        View pView=mInflater.inflate(R.layout.popushow_image,null);
        detail_show_image=(ViewPager)pView.findViewById(R.id.detail_show_image);
        detail_showback_image=(ImageButton)pView.findViewById(R.id.detail_showback_image);
        line_intor=(LinearLayout)pView.findViewById(R.id.line_intor);
        progress_bar=(ProgressBar)pView.findViewById(R.id.progress_bar);
        if(array_img!=null){
            for(int i=0;i<array_img.size();i++){
                ImageView imageView=new ImageView(context_this);
                newbitmapUtils.display(imageView, array_img.get(i),new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                        progress_bar.setVisibility(View.GONE);
                        imageView.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                    }

                    @Override
                    public void onPreLoad(ImageView container, String uri, BitmapDisplayConfig config) {
                        super.onPreLoad(container, uri, config);
                        progress_bar.setVisibility(View.VISIBLE);
                    }
                });

                viewList.add(imageView);
            }
        }
        detail_show_image.setAdapter(new LWNAdapter_home(viewList));
        detail_show_image.setCurrentItem(current);
        line_intor_one=(ImageButton)pView.findViewById(R.id.detail_showback_image);
        line_intor_two=(ImageButton)pView.findViewById(R.id.detail_showback_image);

        showpopupWindow=new PopupWindow(pView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,true);
        showpopupWindow.setBackgroundDrawable(new BitmapDrawable());
        showpopupWindow.setOutsideTouchable(true);
        showpopupWindow.setFocusable(true);
        showpopupWindow.setAnimationStyle(R.style.showImagepopwindow_anim_style);
        showpopupWindow.showAtLocation(line_intor_one, Gravity.CENTER, 0, 0);
        detail_showback_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(showpopupWindow.isShowing()){
                    showpopupWindow.dismiss();
                }
            }
        });
        line_intor.getChildAt(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detail_show_image.setCurrentItem(1);
                line_intor.getChildAt(1).setClickable(false);
                line_intor.getChildAt(0).setClickable(true);
            }
        });

        line_intor.getChildAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detail_show_image.setCurrentItem(0);
                line_intor.getChildAt(0).setClickable(false);
                line_intor.getChildAt(1).setClickable(true);
            }
        });
        if(array_img.size()==1){
            line_intor.getChildAt(1).setVisibility(View.GONE);
        }else if(array_img.size()==2){
            line_intor.getChildAt(1).setVisibility(View.VISIBLE);
        }
        detail_show_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position==0){
                    line_intor.getChildAt(position).setPressed(true);
                    line_intor.getChildAt(position+1).setPressed(false);
                    line_intor.getChildAt(position).setClickable(false);
                    line_intor.getChildAt(position+1).setClickable(true);
                }else{
                    line_intor.getChildAt(position).setPressed(true);
                    line_intor.getChildAt(position-1).setPressed(false);
                    line_intor.getChildAt(position).setClickable(false);
                    line_intor.getChildAt(position-1).setClickable(true);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

